package endpoint

import (
	"context"
	"gowork/studentGoKit/model"
	"github.com/go-kit/kit/endpoint"
	"gowork/studentGoKit/server/service"
)

type SudentNilRequest struct{

}

type SudentRequest struct{
	Id string `json:"id"`
	Name string `json:"name"`
	Birth string `json:"birth"`
	Sex string `json:"sex"`
}

type SudentResponse struct{
	StudentList []*model.Student `json:"studentList"`
}

type StudentNameRequest struct{
	Name string `json:"name"`
}

type StudentMapResponse struct{
	Message string `json:"message"`
	Code string `json:"code"`
}

func StudentInsertEnpoint(ssi service.StudentServiceInf) endpoint.Endpoint{
	return func(ctx context.Context, request interface{}) (response interface{}, err error){
		r := request.(SudentRequest)
		var s = model.Student{
			Id : r.Id,
			Name : r.Name,
			Birth : r.Birth,
			Sex : r.Sex,
		}
		result := ssi.InesrtSri(&s)

		return StudentMapResponse{Message: result["message"], Code : result["code"]}, nil
	}
}

func StudentDeleteEnpoint(ssi service.StudentServiceInf) endpoint.Endpoint{
	return func(ctx context.Context, request interface{}) (response interface{}, err error){
		r := request.(StudentNameRequest)
		result := ssi.DeleteSri(r.Name)

		return StudentMapResponse{Message: result["message"], Code : result["code"]}, nil
	}
}

func StudentUpdateEnpoint(ssi service.StudentServiceInf) endpoint.Endpoint{
	return func(ctx context.Context, request interface{}) (response interface{}, err error){
		r := request.(SudentRequest)
		var s = model.Student{
			Id : r.Id,
			Name : r.Name,
			Birth : r.Birth,
			Sex : r.Sex,
		}
		result := ssi.UpdateSri(&s)

		return StudentMapResponse{Message: result["message"], Code : result["code"]}, nil
	}
}

func StudentQueryEnpoint(ssi service.StudentServiceInf) endpoint.Endpoint{
	return func(ctx context.Context, request interface{}) (response interface{}, err error){
		result := ssi.QuerySri()
		
		return SudentResponse{StudentList: result}, nil
	}
}